//
//  GoodListViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/12.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "GoodListViewController.h"
#import "GoodListCollectionViewCell.h"
#import "ShopInfoViewController.h"
#import "GoodInfoViewController.h"

@interface GoodListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    NSString *s_id;
}

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) SDCycleScrollView *sdcView;
@property (nonatomic,strong) NSArray *dataArray;
- (void)shopInfoEvent;

@end

@implementation GoodListViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"s_id":self.auId,
                                 };
    [[AFAppDotNetAPIClient sharedClient] POST:@"Commodity/selectBysId.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            NSString *urlString = [NSURL URLOffsetIPWithString:responseObject[@"selectBysId"][@"c_first_figure"]].absoluteString;
            s_id = responseObject[@"selectBysId"][@"s_id"];
            self.sdcView.imageURLStringsGroup = @[urlString];
            self.dataArray = responseObject[@"selectBySiD"];
            [self.collectionView reloadData];
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"店铺详情" style:0 target:self action:@selector(shopInfoEvent)];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation GoodListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    self.title = @"店铺首页";
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:({
//        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 24)];
//        [btn setTitle:@"店铺详情" forState:UIControlStateNormal];
//        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        btn.titleLabel.font = DEF_FontSize_12;
//        btn.layer.masksToBounds = YES;
//        btn.layer.cornerRadius = 3;
//        btn.layer.borderWidth = .5;
//        btn.layer.borderColor = [UIColor whiteColor].CGColor;
//        [btn addTarget:self action:@selector(shopInfoEvent) forControlEvents:UIControlEventTouchUpInside];
//        btn;
//        
//    })];
}

#pragma mark - Event

- (void)shopInfoEvent{
    ShopInfoViewController *vc = [ShopInfoViewController new];
    vc.sId = s_id;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([GoodListCollectionViewCell class]) forIndexPath:indexPath];
    cell.dataDictionary = self.dataArray[indexPath.row];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
        [view addSubview:self.sdcView];
    }
    return view;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodInfoViewController *vc = [GoodInfoViewController new];
    vc.gId = self.dataArray[indexPath.row][@"c_id"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set & get

- (SDCycleScrollView *)sdcView{
    if (!_sdcView) {
        _sdcView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160/WIDTH_5S_SCALE - 5)];
    }
    return _sdcView;
}

- (void)setAuId:(NSString *)auId{
    _auId = auId;
    [self getData];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.itemSize = CGSizeMake((SCREEN_WIDTH-5)/2., 135/WIDTH_5S_SCALE);
        layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 160/WIDTH_5S_SCALE);
        
        CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
        _collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = getColor(@"f1f2f6");
        
        [_collectionView registerClass:[GoodListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([GoodListCollectionViewCell class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
    }
    return _collectionView;
}


@end
